﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HCC.Robotics.Services.HCCGenericVisionModule.Proxy;

namespace HCC.Robotics.HCCVisionModule
{
    public partial class CameraCalibration : Form
    {
        FromWinformEvents _fromWinformPort;

        string[] data = new string[17];

        public CameraCalibration(FromWinformEvents EventsPort)
        {
            _fromWinformPort = EventsPort;
            InitializeComponent();
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Loaded, null, this));
        }

        private void btnShutdown_Click(object sender, EventArgs e)
        {
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Shutdown, null));
        }

        private void fieldPicture_MouseClick(object sender, MouseEventArgs e)
        {
            if (DataIsValid.Checked)
                return;

            if (btnTL.Checked)
            {
                data[0] = e.X.ToString();
                data[8] = e.Y.ToString();
                btnTL.Text = "Top Left : " + data[0] + ", " + data[8];
            }
            if (btnTR.Checked)
            {
                data[1] = e.X.ToString();
                data[9] = e.Y.ToString();
                btnTR.Text = "Top Right : " + data[1] + ", " + data[9];
            }
            if (btnBL.Checked)
            {
                data[2] = e.X.ToString();
                data[10] = e.Y.ToString();
                btnBL.Text = "Bottom Left : " + data[2] + ", " + data[10];
            }
            if (btnBR.Checked)
            {
                data[3] = e.X.ToString();
                data[11] = e.Y.ToString();
                btnBR.Text = "Bottom Right : " + data[3] + ", " + data[11];
            }
            if (btnLGU.Checked)
            {
                data[4] = e.X.ToString();
                data[12] = e.Y.ToString();
                btnLGU.Text = "Left Goal Upper : " + data[4] + ", " + data[12];
            }
            if (btnLGL.Checked)
            {
                data[5] = e.X.ToString();
                data[13] = e.Y.ToString();
                btnLGL.Text = "Left Goal Lower : " + data[5] + ", " + data[13];
            }
            if (btnRGU.Checked)
            {
                data[6] = e.X.ToString();
                data[14] = e.Y.ToString();
                btnRGU.Text = "Right Goal Upper : " + data[6] + ", " + data[14];
            }
            if (btnRGL.Checked)
            {
                data[7] = e.X.ToString();
                data[15] = e.Y.ToString();
                btnRGL.Text = "Right Goal Lower : " + data[7] + ", " + data[15];
            }
        }

        private void DataIsValid_CheckedChanged(object sender, EventArgs e)
        {
            if (DataIsValid.Checked)
            {
                data[16] = "true";
                _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.DataCalibrated, data));
            }
            else
            {
                data[16] = "false";
                _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Calibrating, null));
            }
        }

        internal void SetCameraImage(byte[] frame, VisionData _visionData)
        {
            fieldPicture.Image = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Bitmap bmp = (Bitmap)fieldPicture.Image;
            System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
            System.Runtime.InteropServices.Marshal.Copy(frame, 0, bmd.Scan0, frame.Length);
            bmp.UnlockBits(bmd);

            int tmp;
            int.TryParse(data[0], out tmp);
            _visionData.TopLeftX = tmp;
            int.TryParse(data[1], out tmp);
            _visionData.TopRightX = tmp;
            int.TryParse(data[2], out tmp);
            _visionData.BottomLeftX = tmp;
            int.TryParse(data[3], out tmp);
            _visionData.BottomRightX = tmp;

            int.TryParse(data[4], out tmp);
            _visionData.LeftGoalUpperX = tmp;
            int.TryParse(data[5], out tmp);
            _visionData.LeftGoalLowerX = tmp;

            int.TryParse(data[6], out tmp);
            _visionData.RightGoalUpperX = tmp;
            int.TryParse(data[7], out tmp);
            _visionData.RightGoalLowerX = tmp;

            int.TryParse(data[8], out tmp);
            _visionData.TopLeftY = tmp;
            int.TryParse(data[9], out tmp);
            _visionData.TopRightY = tmp;
            int.TryParse(data[10], out tmp);
            _visionData.BottomLeftY = tmp;
            int.TryParse(data[11], out tmp);
            _visionData.BottomRightY = tmp;

            int.TryParse(data[12], out tmp);
            _visionData.LeftGoalUpperY = tmp;
            int.TryParse(data[13], out tmp);
            _visionData.LeftGoalLowerY = tmp;

            int.TryParse(data[14], out tmp);
            _visionData.RightGoalUpperY = tmp;
            int.TryParse(data[15], out tmp);
            _visionData.RightGoalLowerY = tmp;

            if (data[16] == "true")
                _visionData.Valid = true;
            else _visionData.Valid = false;

            // TODO: BB finding ball and players.

            fieldPicture.Invalidate();
        }

        internal void SetErrorImage(System.Drawing.Size size)
        {
            Bitmap bmp = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics grfx = Graphics.FromImage(bmp);
            Font font = new System.Drawing.Font(FontFamily.GenericSerif, 12);
            Brush brush = Brushes.Yellow;
            grfx.DrawString(string.Format("The size of the picture is wrong ({0}:{1}).", size.Width, size.Height), font, brush, new PointF(10, 240));
            grfx.DrawString("Use the webbrowser to correct the webcam image format to (640:480).", font, brush, new PointF(10, 254));
            fieldPicture.Image = bmp;
            fieldPicture.Invalidate();
        }
    }
}
